package com.kuanter.kuanterauto.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.kuanter.kuanterauto.activity.MerchantTurnActivity1;
import com.kuanter.kuanterauto.activity.ShareActivity;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
	IWXAPI wx;
	
	int who;
	String saveMoney;
	
	String phone;
	String name;
	String address;
	
	String text = "";
	boolean friengship;
	
	String title;
	String pushTime;
	String content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(MerchantTurnActivity1.sh) {
			finish();
		}
		Log.i("ccc", "启动了");
		wx = WXAPIFactory.createWXAPI(this, MainConstants.WEIXIN_APP_ID, false);
		wx.registerApp(MainConstants.WEIXIN_APP_ID);
		wx.handleIntent(getIntent(), this);
		
		Intent intent = getIntent();
		saveMoney = intent.getStringExtra("saveMoney");
		who = intent.getIntExtra("who", 0);
		
		phone = intent.getStringExtra("phone");
		name = intent.getStringExtra("name");
		address = intent.getStringExtra("address");
		friengship = intent.getBooleanExtra("friend", false);
		
		title = intent.getStringExtra("title");
		pushTime = intent.getStringExtra("pushTime");
		content = intent.getStringExtra("content");
		
		if(!wx.isWXAppInstalled()) {
			Toast.makeText(this, "您没有安装微信", Toast.LENGTH_SHORT).show();
			finish();
		}
		
		Log.i("cca", MerchantTurnActivity1.sh + "---");
		if(!MerchantTurnActivity1.sh) {
			MerchantTurnActivity1.sh = true;
			if (who == ShareActivity.MY) {
				if (phone != null && name != null && address != null) {
					text = "我在宽途网发现一家不错的店，快来看看吧：" + name + ";\n" + "地址："
							+ address + ";\n" + "电话：" + phone + ";\n";
					/*boolean success = false;
					WXTextObject textObj = new WXTextObject();
					textObj.text = text;
					WXMediaMessage msg = new WXMediaMessage();
					msg.mediaObject = textObj;
					msg.description = text;
					SendMessageToWX.Req req = new SendMessageToWX.Req();
					req.transaction = buildTransaction("text"); //
					req.message = msg;
					// req.scene = isTimelineCb.isChecked() ?
					// SendMessageToWX.Req.WXSceneTimeline :
					// SendMessageToWX.Req.WXSceneSession;

					if (friengship) {
						req.scene = SendMessageToWX.Req.WXSceneTimeline;
						if (!apiSupport()) {
							Toast.makeText(WXEntryActivity.this,
									"您的微信版本不支持分享到朋友圈", Toast.LENGTH_SHORT)
									.show();
							finish();
						}
					} else {
						req.scene = SendMessageToWX.Req.WXSceneSession;
					}
					success = wx.sendReq(req);*/
				} else {
					finish();
				}
			} else if(who == ShareActivity.YOU){
				/*if(saveMoney != null) {
					text = "宽途已为我节省" + saveMoney + "元";
				}*/
				
				if(title != null && pushTime != null && content != null) {
					text = "宽途消息：" + title + ";\n" + "时间：" + pushTime
							+ ";\n" + "内容：" + content + ";\n";
				}
			} else {
				if(saveMoney != null) {
					text = "宽途已为我节省" + saveMoney + "元";
				}
			}
			
			boolean success = false;
			WXTextObject textObj = new WXTextObject();
			textObj.text = text;
			WXMediaMessage msg = new WXMediaMessage();
			msg.mediaObject = textObj;
			msg.description = text;
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("text"); //
			req.message = msg;
			// req.scene = isTimelineCb.isChecked() ?
			// SendMessageToWX.Req.WXSceneTimeline :
			// SendMessageToWX.Req.WXSceneSession;

			if (friengship) {
				req.scene = SendMessageToWX.Req.WXSceneTimeline;
				if (!apiSupport()) {
					Toast.makeText(WXEntryActivity.this,
							"您的微信版本不支持分享到朋友圈", Toast.LENGTH_SHORT)
							.show();
					finish();
				}
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			Log.i("ccc", "11111111");
			success = wx.sendReq(req);
			Log.i("ccc", "2222222222");
			finish();
		} else {
			finish();
		}
	}

	@Override
	public void onReq(BaseReq req) {
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
//			goToGetMsg();	
			finish();
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
//			goToShowMsg((ShowMessageFromWX.Req) req);
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.i("ccc", resp.errCode + "errorCode");
		int result = 0;
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
//			result = R.string.errcode_success;
			Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show();
			finish();
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
//			result = R.string.errcode_cancel;
//			Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
			finish();
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
//			result = R.string.errcode_deny;
			finish();
			break;
		default:
//			result = R.string.errcode_unknown;
			Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show();
			finish();
			break;
		}
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		
		setIntent(intent);
        wx.handleIntent(intent, this);
	}
	
	@Override
	protected void onDestroy() {
		Log.i("ccc", "finish...");
		super.onDestroy();
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
	}
	
	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
	public boolean apiSupport() {
		int wxSdkVersion = wx.getWXAppSupportAPI();
		if (wxSdkVersion >= TIMELINE_SUPPORTED_VERSION) {
			return true;
		} else {
			return false;
		}
	}

}
